× Lesson 1 Lesson 2 Lesson 3 Lesson 4 Lesson 5 Lesson 6 Lesson 7 Lesson 8 Lesson 9 Lesson 10 Lesson 11 Lesson 12 Lesson 13 Lesson 14 Lesson 15 Lesson 16 Lesson 17 Lesson 18 Lesson 19 Lesson 20 Lesson 21 Lesson 22 Lesson 23 Lesson 24 Lesson 25 Mini Lesson 1 Mini Lesson 2 Mini Lesson 3 Mini Lesson 4 Mini Lesson 5

Lessons

Lesson 9 - Logical Operators

The basic logical operators are: and, or, not. They are all very useful in conjunction with conditions and in if statements. The first operator is 'or'. Or compares two statements, and if atleast one of them is true, then both the two statements and or are considered 'True'. Here's an example:

print(len('John') == 4 or False)

This would print out True as even though there is a 'False', the length of 'John' is equal to 4, so the print() prints True.

The next operator is 'and'. And, unlike or, requires both statements to be True for it to print True, otherwise it will return False. For example:

print(len('John') == 4 and False)

This code would print out False as even though the length of 'John' is 4, there is a False, so the print() would print out False.

The final operator is 'not'. Not is not used the same like and or or, instead it used to modify conditions. It turns False to True and True to False. For example:

print(not False)

This would output True, as True is the opposite of False.

Logical operators will come in handy, especially regarding if statements and while Loops.